package ecom.stateless;

import javax.ejb.MessageDriven;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;



import javax.activation.DataSource;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.*;
import javax.naming.InitialContext;


/*name = "Demande", 
activationConfig={
		@ActivationConfigProperty(
				propertyName="destinationType",
				propertyValue="javax.jms.Queue")}
		

)
 * @ActivationConfigProperty(
				propertyName="destinationName",
				propertyValue="sampleQueue")
 *
 */



/*
 (
		name = "test", 
		activationConfig={
				@ActivationConfigProperty(
						propertyName="destinationType",
						propertyValue="javax.jms.Queue"),
				@ActivationConfigProperty(
						propertyName="destinationName",
						propertyValue="sampleQueue")
		}
	)
 */


@MessageDriven(mappedName="sampleQueue")

public class TextListener implements MessageListener {
    /**
     * Casts the message to a TextMessage and displays its text.
     *
     * @param message     the incoming message
     */
	
	
	//@Resource(mappedName = "jms/ConnectionFactory")
    private static ConnectionFactory connectionFactory;
    //@Resource(mappedName = "jms/Queue")
    private static Queue queue;
    String destType = null;
    Connection connection = null;
    Session session = null;
    Destination dest = null;
    MessageConsumer consumer = null;
   
    TextMessage message = null;
 
    
    
    
	
	public TextListener()
	{
		 System.out.println(" consumer started 1");
	    	
	    	
	    	
	    	try{
		    	InitialContext initCtx = new InitialContext();
		        // Perform JNDI lookup to obtain the resource.
		    	queue = (Queue)
		    			initCtx.lookup("sampleQueue");
		    	}catch(Exception e)
		    	{
		    		System.out.println("got Exception 2" + e.getMessage());
		    	}
		
	    		
	    	dest = (Destination) queue;
	    	
	    	try {
	    		try{
	    	    	InitialContext initCtx = new InitialContext();
	    	        // Perform JNDI lookup to obtain the resource.
	    	    	connectionFactory = (ConnectionFactory)
	    	    			initCtx.lookup("CF");
	    	    	}catch(Exception e2)
	    	    	{
	    	    		System.out.println("got Exception 2" + e2.getMessage());
	    	    	}
	    		 connection = connectionFactory.createConnection();
	           
	    		 System.out.println("consumer started 2");
	    		 connection.start();
	    		 System.out.println("consumer started 3");
	    		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	    		System.out.println("consumer started 4");
	            consumer = session.createConsumer(dest);
	            System.out.println("consumer started 5");
	            
	    	}catch(Exception e)
	    	{
	    		System.out.println("Error 100!!!!!");
	    	}

		
	}

	
	
    public void onMessage(Message message) {
        TextMessage msg = null;

        try {
        	System.out.println("TextListener");
            if (message instanceof TextMessage) {
                msg = (TextMessage) message;
                System.out.println("Reading message: " + msg.getText());
            } else {
                System.err.println("Message is not a TextMessage");
            }
        } catch (JMSException e) {
            System.err.println("JMSException in onMessage(): " + e.toString());
        } catch (Throwable t) {
            System.err.println("Exception in onMessage():" + t.getMessage());
        }
    }
}

